// pages/mine/edtAddr.js
const app = getApp();
import areaCode from '../../utils/area_code.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelected:true,
    //=====
    isShow: false,
    areaList: {
      province_list: areaCode.list.province_list,
      city_list: areaCode.list.city_list,
      county_list: areaCode.list.county_list
    },
    province:{},
    city: {},
    area: {},
    address_value: '180102',
    id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.util.isUndefined(options.id)) {
      this.setData({
        id: options.id
      })
      this.getAddress(options.id);
    }
  },

  getAddress(id) {
    app.http.$ajax({
      url: 'v1.niu/getAddress',
      data: {
        id: id
      }
    }, {
      success: (res) => {
        this.setData({
          userName: res.data.name,
          phone: res.data.mobile,
          userName: res.data.name,
          province: res.data.province_extra,
          city: res.data.city_extra,
          area: res.data.area_extra,
          address_value: res.data.area_extra.code,
          isSelected: res.data.is_default,
          detail: res.data.detail
        })
      }
    });
  },
  onClose() {
    this.setData({
      isShow: false
    })
  },
 
  bindPicker:function(){
    this.setData({
      isShow:true,
    });
  },
 
  getValues(v) {
    let data = v.detail.values;
    this.setData({
      province: data[0],
        city: data[1],
      area: data[2],
      isShow: false,
      address_value: data[2] ? data[2].code : '050100'
    })
  },
  save:function(e){
    let values = e.detail.value;
    let post = {
      name: values.userName,
      mobile: values.phone,
      detail: values.detail,
      province: this.data.province.code,
      city: this.data.city.code,
      area: this.data.area.code,
      is_default: values.addrSelect ? 1 : 0
    }
    if(this.data.id) {
      post.id = this.data.id
    }
    app.http.$ajax({
      url: 'v1.address/add',
      data: post,
      loading: '正在保存...'
    }, {
      success:(res) => {
        app.success(res.msg);
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000)
      }
    })
  },
  del:function(){
    let that = this;
    wx.showModal({
      title: '确定要删除吗？',
      content: '删除之后无法恢复',
      showCancel: true,
      success: function (res) {
        app.http.$ajax({
          url: 'v1.address/del',
          data: {
            id: that.data.id
          }
        }, {
            success(res) {
              app.success('删除成功');
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }
          });
      }
    });
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})